<?php
/**
 * This file is part of Swoft.
 *
 * @link https://swoft.org
 * @document https://doc.swoft.org
 * @contact group@swoft.org
 * @license https://github.com/swoft-cloud/swoft/blob/master/LICENSE
 */

namespace App\Middlewares;

use Psr\Http\Server\RequestHandlerInterface;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use Swoft\Bean\Annotation\Bean;
use Swoft\Http\Message\Middleware\MiddlewareInterface;
// use Swoft\Core\RequestContext;


/**
 * @Bean()
 * @uses      ActionTestMiddleware
 * @version   2017年11月16日
 * @author    huangzhhui <huangzhwork@gmail.com>
 * @copyright Copyright 2010-2017 Swoft software
 * @license   PHP Version 7.x {@link http://www.php.net/license/3_0.txt}
 */
class ActionTestMiddleware implements MiddlewareInterface
{

    /**
     * Process an incoming server request and return a response, optionally delegating
     * response creation to a handler.
     *
     * @param \Psr\Http\Message\ServerRequestInterface $request
     * @param \Psr\Http\Server\RequestHandlerInterface $handler
     * @return \Psr\Http\Message\ResponseInterface
     * @throws \InvalidArgumentException
     */
    public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface
    {
        $request->addParserBody('test', 'Mike Rust');
        // $a = RequestContext::getRequest();

        \Jpay\Helper\wk_monolog(['msg' => 'test', 'data' => ['get' => $request->getUri()->getQuery(), 'post' => $request->raw(), 'ip' => $request->getHeaderLine('X-Real-IP')]], 'merchant/test');

        $response = $handler->handle($request);

        $response = $response->withoutHeader('X-Powered-By');
        $response = $response->withAddedHeader('X-Powered-By', 'J-PAY');

        return $response->withAddedHeader('Middleware-Action-Test', 'success');
    }
}